﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotvaBot.RequestChainItems;
using Helpers;
using BotvaBot.RequestChains.Base;

namespace BotvaBot.RequestChains
{
    public class GetPersonajInfoChain : BotvaChainBase
    {
        public GetPersonajInfoChain()
            : base()
        { }

        public GetPersonajInfoChain(BotvaChainContext parentContext)
            : base(parentContext)
        { }



        public override void Init()
        {
            base.Init();

            AddItem(new EnterChainItem(), () => !WithInheritedContext);
            AddItem(new LoginChainItem(), () => !WithInheritedContext);
            AddItem(new GotoPersonajChainItem());
            AddItem(new LogoutChainItem(), () => !WithInheritedContext);
        }

        public string GetPersonajPageContent()
        {
            GotoPersonajChainItem chainItem = Chain.Values.ToList()
                .Where(item => item.GetType() == typeof(GotoPersonajChainItem))
                .FirstOrDefault() as GotoPersonajChainItem;

            if (chainItem == null)
                throw new Exception("Chain item EnterChainItem can not be found");

            if (chainItem.Requester == null)
                throw new Exception("Requester is null for Chain item EnterChainItem");

            if (StringHelper.IsnullOrBlank(chainItem.Requester.PageContent))
                return null;

            return chainItem.Requester.PageContent;
        }
    }
}
